<?php
namespace jy_cong2fan;
defined('BY_JYA') or exit('error');
/*
	分红插件工具类
*/
class model extends \plugin{
	
	public function create_condition($conditions){
		if(!$conditions){
			return '';
		}
		if(!is_array($conditions)){
			$conditions=json_decode($conditions,true);
		}
		$level_up_types=$this->level_up_types;
		$current_level_up_types=$this->current_level_up_types();
		$str='';
		foreach($current_level_up_types as $key){
			$str.=$level_up_types[$key].'达'.$conditions[$key].';';
		}
		return $str;
	}
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function otherfee_types(){
		$jy_renwushop = check_permission('jy_renwushop');
		$res=array(
			'0'=>'无',
		);
		if($jy_renwushop){
			$res['jy_renwushop']='任务商城分佣';
		}
		return $res;
	}
	public function get_area_code($area){
		if(!$area['province']){
			return '';
		}
		$address=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
		}
		if($area['district']){
			$address.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if($isup==0){
				require ROOT_D.'/update/update_area.php';
			}
		}
		return $area_code;
	}
	//获取昨日营业额
	public function get_dayfeelog(){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$yesdaytime=$daytime-24*3600;
		$yesday=date('Y-m-d',$yesdaytime);
		$feelog=pdo_get('jy_cong2fan_feelog',array('uniacid'=>$_SESSION['uniacid'],'day'=>$yesday,'type'=>2));
		if(!$feelog['id']){
			$feelog['orderfee']=$this->up_dayfeelog();
		}
		return $feelog['orderfee'];
	}
	//营业额入池
	public function up_dayfeelog(){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$yesdaytime=$daytime-24*3600;
		$yesday=date('Y-m-d',$yesdaytime);
		$feelog=pdo_get('jy_cong2fan_feelog',array('uniacid'=>$_SESSION['uniacid'],'day'=>$yesday,'type'=>2));
		if($feelog['id']){
			return false;
		}
		$allfee=pdo_getsum('jy_cong2fan_order',array('uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($yesdaytime,$daytime)),'commission_price');
		if($this->setting['orderfee']){
			$allfee=$allfee+$this->setting['orderfee'];
		}
		$fee_rate=$this->setting['orderfee_rate'];
		$allredfee=$allfee*$fee_rate/100;
		$feelog=array(
					'uniacid'=>$_SESSION['uniacid'],
					'year'=>date('Y',$yesdaytime),
					'day'=>$yesday,
					'orderfee_rate'=>$fee_rate,
					'orderfee'=>$allfee,
					'redfee'=>$allredfee,
					'createtime'=>TIMESTAMP,
					'type'=>2,
		);
		$this->insert('feelog',$feelog);
		return $allfee;
	}
	public function up_redfee(){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$yesdaytime=$daytime-24*3600;
		$yesday=date('Y-m-d',$yesdaytime);
		$feelog=pdo_get('jy_cong2fan_feelog',array('uniacid'=>$_SESSION['uniacid'],'day'=>$yesday,'type'=>2));
		if($feelog['id']){
			return false;
		}
		$allfee=$this->get_dayfeelog();
		if($allfee<=0 || !$this->setting['orderfee_rate']){
			return false;
		}
		$levelids=$this->getall('level',array('uniacid'=>$_SESSION['uniacid'],'isdong'=>1),'id','id');
		if($levelids){
			$levelids=array_keys($levelids);
		}else{
			return false;
		}
		$m_params=array('stop'=>0,'is_agent'=>1,'level_id'=>$levelids,'uniacid'=>$_SESSION['uniacid']);
		$this->up_memberfee($allfee,$this->setting['orderfee_rate'],$m_params,date('Y',$yesdaytime),$yesday,3);
		return true;
	}
	public function up_memberfee($allfee=0,$fee_rate=0,$m_params=array(),$year='',$day='',$type=3){
		if($allfee<=0 || !$fee_rate){
			return false;
		}
		$allredfee=$allfee*$fee_rate/100;
		$allrednum=pdo_getsum('jy_cong2fan_member',$m_params,'rednum');
		$redfee=$allredfee/$allrednum;
		$redfee=intval($redfee*100)/100;
		$members=pdo_getall('jy_cong2fan_member',$m_params);
		$m_num=count($members);
		$feelog=array(
					'uniacid'=>$_SESSION['uniacid'],
					'year'=>$year,
					'day'=>$day,
					'orderfee_rate'=>$fee_rate,
					'orderfee'=>$allfee,
					'redfee'=>$allredfee,
					'm_num'=>$m_num,
					'z_num'=>$allrednum,
					'createtime'=>TIMESTAMP,
					'type'=>$type,
		);
		$this->insert('feelog',$feelog);
		$id=$this->pdo_insertid();
		foreach($members as $mv){
			$myredfee=$redfee;
			if($myredfee>0){
				$feelog=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$mv['mid'],
					'ordersn'=>'feelog'.$id,
					'type'=>5,
					'money'=>$myredfee,
					'createtime'=>TIMESTAMP,
					'day'=>$day,
					'status'=>1,
				);
				$this->insert('commission',$feelog);
				cfc('core')->member_credit($feelog['mid'],'credit2',$myredfee,1,'jy_cong2fan','业绩分红');
			}
		}
		return true;
	}
}